﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.UI.HtmlControls;

namespace Department.admin.SiteStructure
{
    public partial class AddPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Request.IsAuthenticated)
            {
                Response.Redirect("/admin/Login.aspx");
            }
            this.Page.Title = "Добави страница";

            AdminVariablesSingleton.changeCSS(Master, "struct");
            AdminVariablesSingleton.configureCKEditor(ref TextBoxText);
                     
        }

      
        protected void ButtonSave_Click(object sender, EventArgs e)
        {
        
            

            string strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["DepartmentConnectionString"].ConnectionString;
            SqlConnection conn = new SqlConnection(strConnString);
            try
            {
                conn.Open();
                string sqlCommandText = @"INSERT INTO Menu (Name, ParentID, FacDeptID) Values (@MenuName, @ParentMenu, dbo.uf_get_department_id()); INSERT INTO Pages (Name, SiteContent, IsMain, TypeID, MenuID) VALUES (@Heading, @Text, @IsMain, @Type, @@Identity)";
                SqlCommand saveToDB = new SqlCommand(sqlCommandText, conn);

                saveToDB.Parameters.AddWithValue("@Heading", TextBoxHeading.Text);
                saveToDB.Parameters.AddWithValue("@Text", TextBoxText.Text);
                saveToDB.Parameters.AddWithValue("@IsMain", CheckBoxIsMain.Checked);
                Object value;
                if (DropDownListMenu.SelectedValue.Equals("NULL"))
                {
                    value = Convert.DBNull;
                }
                else
                {
                    value = DropDownListMenu.SelectedValue;
                }
                saveToDB.Parameters.AddWithValue("@ParentMenu", value);
                saveToDB.Parameters.AddWithValue("@MenuName", TextBoxListMenu.Text);
                if (DropDownListType.SelectedValue.Equals("NULL"))
                {
                    value = Convert.DBNull;
                }
                else
                {
                    value = DropDownListType.SelectedValue;
                }
                saveToDB.Parameters.AddWithValue("@Type", value);

                saveToDB.ExecuteNonQuery();

                sqlCommandText = "SELECT MAX(ID) FROM Pages";
                saveToDB.CommandText = sqlCommandText;
                
                int pageID = Convert.ToInt16(saveToDB.ExecuteScalar());


                

                foreach (GridViewRow row in rolesGridView.Rows)
                {        
                    if (((CheckBox)row.Cells[1].Controls[1]).Checked)
                    {
                        sqlCommandText = "INSERT INTO PagesForRoles (PageID, RoleID) Values (@PageID, @RoleID)";
                        SqlCommand saveToDB2 = new SqlCommand(sqlCommandText, conn);
                        saveToDB2.Parameters.AddWithValue("@PageID", pageID);
                        saveToDB2.Parameters.AddWithValue("@RoleID", row.Cells[2].Text);
                        saveToDB2.CommandText = sqlCommandText;
                        saveToDB2.ExecuteNonQuery();
                       

                    }
                }
            }
            catch (SqlException sqle)
            {
                Response.Redirect("/admin/ErrorPage.aspx?type=sql&err=" + sqle.Message);
            }
            catch (Exception ex)
            {
                Response.Redirect("/admin/ErrorPage.aspx?type=1&err=" + ex.Message);
            }
            finally
            {
                conn.Close();
            }
            
            Response.Redirect("/admin/SiteStructure/ViewAllPages.aspx");
        }

        protected void ButtonReset_Click(object sender, EventArgs e)
        {
            TextBoxHeading.Text = "";
            TextBoxText.Text = "";
        }
        
    }
}